package com.itheima.controller;

import com.alibaba.dubbo.config.annotation.Reference;
import com.itheima.constant.MessageConstant;
import com.itheima.entity.PageResult;
import com.itheima.entity.QueryPageBean;
import com.itheima.entity.Result;
import com.itheima.pojo.Menu;
import com.itheima.service.MenuService;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName MenuController
 * @Description
 * @Author Jingxiao.Ye
 * @Date 2020/1/14 10:24
 * @Version V1.0
 * @Since JDK 1.8
 */
@RestController
@RequestMapping("/menu")
public class MenuController {

    @Reference
    private MenuService menuService;

    /** 
    * @Description: 查询所有信息 
    * @Param:  
    * @return:  
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/14 
    */ 
    @RequestMapping("/findByPage")
    public PageResult findByPage(@RequestBody QueryPageBean queryPageBean){
        PageResult page = menuService.findPage(queryPageBean);
        return page;
    }


    /**
    * @Description:  根据菜单id获得Result
    * @Param:  menu_id
    * @return:
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    @RequestMapping("/findRoleByMenuId")
    public Result findRoleByMenuId(Integer id){
        try {
            Map role = menuService.findRoleByMenuId(id);
            return new Result(true,MessageConstant.GET_ROLE_SUCCESS,role);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_ROLE_FAIL);
        }
    }


    /**
    * @Description: 根据传入的Role的list获得RoleId列表,根据roleId列表和menuId修改
    * @Param:
    * @return:
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    @RequestMapping("/modifyMenuRole")
    public Result modifyMenuRole(@RequestBody List<Map<String, Object>> list,Integer menuId){
        try {
            ArrayList<Integer> arrayList = new ArrayList<>();
            for (Map<String, Object> stringObjectMap : list) {
                arrayList.add((Integer) stringObjectMap.get("id"));
            }
            menuService.modifyMenuRole(menuId,arrayList);
            return new Result(true,MessageConstant.MODIFY_ROLE_MENU_SUCCESS);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.MODIFY_ROLE_MENU_FAIL);
        }
    }


    /**
    * @Description: 通过用户名查询菜单
    * @Param:
    * @return:
    * @Author: Jingxiao.Ye
    * @Date: 2020/1/16
    */
    @RequestMapping("/findMenuByUsername")
    public Result findMenuByUsername(String username){
        try {
            List<Menu> menus = menuService.findMenuByUsername(username);
            return new Result(true,MessageConstant.GET_MENU_SUCCESS,menus);
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false,MessageConstant.GET_MENU_FAIL);
        }
    }
}
